local PortalMainFrame = {}
local UIUtils = PortalUIUtils
local JSON = PortalJSON
local Utils = PortalUtils
local AceGUI = LibStub("AceGUI-3.0")

local mapTypes = {
  [0] =	"Cosmic",
  [1] =	"World",
  [2] =	"Continent",
  [3] =	"Zone",
  [4] =	"Dungeon",
  [5] =	"Micro",
  [6] =	"Orphan"
}

function PortalMainFrame:Create()
  -- 创建一个窗口
  local frame = AceGUI:Create("Window")
  frame:SetTitle("我的插件")
  frame:SetLayout("Flow")
  frame:SetWidth(960)
  frame:SetHeight(600)
  frame:SetCallback("ADDON_LOADED", function()
    -- 获取当前地图类型
    UiMapType = GetUiMapType()
  end)
  frame:SetCallback("OnClose", function(widget) AceGUI:Release(widget) end)

  local UiMapType = nil

  -- local spellInput =  UIUtils.CreateInput("spellID:", 380, frame)
  -- local affixInput =  UIUtils.CreateInput("affixID:", 380, frame)
  local flex1 = UIUtils.CreateGroup(frame)
  local uiMapIDIpt = UIUtils.CreateInput("uiMapID", 0.4, flex1)
  local uiMapIDBtn = UIUtils.CreateButton("获取uiMapID", 0.3, flex1)
  local uiInfoBtn = UIUtils.CreateButton("获取信息", 0.3, flex1)

  local flex2 = UIUtils.CreateGroup(frame)
  local dropdown = UIUtils.CreateDropdown(0.4, mapTypes, flex2)
  local uiInfosBtn = UIUtils.CreateButton("获取子地图", 0.3, flex2)
  local taxiNodesBtn = UIUtils.CreateButton("获取飞行点", 0.3, flex2)

  local jsonBox = UIUtils.CreateTextArea("输出", 420, frame)

  local flex3 = UIUtils.CreateGroup(frame)
  local mapLog = UIUtils.CreateButton("获取所有地图", 0.5, flex3)
  local taxiLog = UIUtils.CreateButton("获取所有飞行点", 0.5, flex3)

  local function getUiMapID()
    local uiMapID = tonumber(uiMapIDIpt:GetText())
    if not uiMapID then
      print("请输入一个有效的uiMapID")
      return { error = true, value = nil }
    end
    return { error = false, value = uiMapID }
  end

  -- 获取当前角色所在 UI map
  uiMapIDBtn:SetCallback("OnClick", function()
    local uiMapID = C_Map.GetBestMapForUnit("player")
    uiMapIDIpt:SetText(uiMapID)
  end)

  -- 获取指定 UI map 的信息
  uiInfoBtn:SetCallback("OnClick", function()
    local result = getUiMapID()
    if (result.error) then
      return
    end

    local uiMapDetails = C_Map.GetMapInfo(result.value)
    Portal:SaveMapData(uiMapDetails)
    jsonBox:SetText(JSON.Encode(uiMapDetails))
  end)

  -- 获取指定地图的子地图信息
  -- https://warcraft.wiki.gg/wiki/API_C_Map.GetMapChildrenInfo
  uiInfosBtn:SetCallback("OnClick", function()
    local result = getUiMapID()
    if (result.error) then
      return
    end
    local childrenUiMapDetails = C_Map.GetMapChildrenInfo(result.value, UiMapType)
    Portal:SaveMapData(childrenUiMapDetails)
    jsonBox:SetText(JSON.Encode(childrenUiMapDetails))
  end)

  -- 获取指定地图的飞行点
  -- https://warcraft.wiki.gg/wiki/API_C_TaxiMap.GetAllTaxiNodes
  taxiNodesBtn:SetCallback("OnClick", function()
    local result = getUiMapID()
    if (result.error) then
      return
    end

    local taxiMapId = GetTaxiMapID()

    if taxiMapId == nil then
      print("请打开飞行点地图")
      return
    end

    -- 飞行点的坐标获取两个条件：
    -- 1. 打开飞行点地图
    -- 2. 给定当前地图 uiMapID，比如多恩岛（可以是父级“卡兹阿加”、可以是子级“多恩诺嘉尔”，但这两个意义不大，一个太大，看不到鸟点，一个太小，只有这一个鸟点）
    local taxiNodes = C_TaxiMap.GetAllTaxiNodes(result.value)
    Portal:SaveTaxiData(taxiNodes)
    jsonBox:SetText(JSON.Encode(taxiNodes))

    -- 鸟点坐标直接 * 100 即可
    -- local normalizedX, normalizedY = taxiNodes[2].position:GetXY()
    -- print(normalizedX * 100, normalizedY * 100)
  end)

  dropdown:SetCallback("OnValueChanged", function(widget, event, value)
    UiMapType = value
    print(string.format("名称：%s，uiMapType: %d", mapTypes[value], UiMapType))
  end)

  mapLog:SetCallback("OnClick", function()
    local mapData = Portal:GetMapData()
    jsonBox:SetText(JSON.Encode(mapData))
  end)
end

_G["PortalMainFrame"] = PortalMainFrame